package com.elder.business.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.ElderFamiliesMapper;
import com.elder.business.domain.ElderFamilies;
import com.elder.business.service.IElderFamiliesService;

/**
 * 老人家庭联系Service业务层处理
 * 
 * @author zyk
 * @date 2023-11-26
 */
@Service
public class ElderFamiliesServiceImpl implements IElderFamiliesService 
{
    @Autowired
    private ElderFamiliesMapper elderFamiliesMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    /**
     * 查询老人家庭联系
     * 
     * @param id 老人家庭联系主键
     * @return 老人家庭联系
     */
    @Override
    public JSONObject selectElderFamiliesById(Long id) {
        ElderFamilies v = elderFamiliesMapper.selectElderFamiliesById(id);
        //获取老人信息
        if(Objects.isNull(v)){
            return new JSONObject();
        }
        ElderlyInfo elderInfo = elderlyInfoMapper.getElderInfoByElderId(v.getElderId());
       JSONObject obj = new JSONObject();
        obj.put("id", StringNewUtils.formatStr(v.getId()));
        obj.put("elderId", StringNewUtils.formatStr(v.getElderId()));
        obj.put("elderName", Objects.isNull(v.getElderId()) ? "" : elderInfo.getElderName());
        obj.put("familiesId", StringNewUtils.formatStr(v.getFamiliesId()));
        obj.put("familiesName", v.getFamiliesName());
        obj.put("familiesPhone", v.getFamiliesPhone());
        obj.put("relationship", v.getRelationship());
        obj.put("familiesAddress", v.getFamiliesAddress());
        return obj;
    }

    @Override
    public List<ElderFamilies> selectElderFamiliesList(ElderFamilies elderFamilies) {
        return elderFamiliesMapper.selectElderFamiliesList(elderFamilies);
    }

    /**
     * 查询老人家庭联系列表
     *
     * @return 老人家庭联系
     */
    @Override
    public TableDataInfo getElderFamiliesList(String elderName, Integer pageNum, Integer pageSize)
    {
        List<Long> elderIds = null;
        if(!StringNewUtils.isEmpty(elderName)){
            List<ElderlyInfo> elderinfoList = elderlyInfoMapper.getElderinfoList(elderName);
            elderIds = elderinfoList.stream().map(ElderlyInfo::getElderId).collect(Collectors.toList());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<ElderFamilies> list = elderFamiliesMapper.getElderFamiliesList(elderIds);
        PageInfo<ElderFamilies> pageInfo = new PageInfo<>(list);
        List<ElderFamilies> familiesList = pageInfo.getList();
        List<Long> elderIdList = familiesList.stream().map(ElderFamilies::getElderId).collect(Collectors.toList());
        List<ElderlyInfo> elderlyInfos = elderlyInfoMapper.getElderInfoByElderIds(elderIdList);
        Map<Long, String> elderMap = elderlyInfos.stream().collect(Collectors.toMap(ElderlyInfo::getElderId, ElderlyInfo::getElderName));
        List<JSONObject> dataList = familiesList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("id", StringNewUtils.formatStr(v.getId()));
            obj.put("elderId", StringNewUtils.formatStr(v.getElderId()));
            obj.put("elderName", Objects.isNull(v.getElderId()) ? "" : elderMap.get(v.getElderId()));
            obj.put("familiesId", StringNewUtils.formatStr(v.getFamiliesId()));
            obj.put("familiesName", v.getFamiliesName());
            obj.put("familiesPhone", v.getFamiliesPhone());
            obj.put("relationship", v.getRelationship());
            obj.put("familiesAddress", v.getFamiliesAddress());
            return obj;
        }).collect(Collectors.toList());
        TableDataInfo data = new TableDataInfo();
        data.setCode(200);
        data.setMsg("success");
        data.setTotal(pageInfo.getTotal());
        data.setRows(dataList);
        return data;
    }

    /**
     * 新增老人家庭联系
     * 
     * @param elderFamilies 老人家庭联系
     * @return 结果
     */
    @Override
    public int insertElderFamilies(ElderFamilies elderFamilies)
    {
        elderFamilies.setId(SnowGenerator.getId());
        elderFamilies.setCreateTime(DateUtils.getLongTime());
        return elderFamiliesMapper.insertElderFamilies(elderFamilies);
    }

    /**
     * 修改老人家庭联系
     * 
     * @param elderFamilies 老人家庭联系
     * @return 结果
     */
    @Override
    public int updateElderFamilies(ElderFamilies elderFamilies)
    {
        elderFamilies.setUpdateTime(DateUtils.getLongTime());
        return elderFamiliesMapper.updateElderFamilies(elderFamilies);
    }

    /**
     * 批量删除老人家庭联系
     * 
     * @param ids 需要删除的老人家庭联系主键
     * @return 结果
     */
    @Override
    public int deleteElderFamiliesByIds(Long[] ids)
    {
        return elderFamiliesMapper.deleteElderFamiliesByIds(ids);
    }

    /**
     * 删除老人家庭联系信息
     * 
     * @param id 老人家庭联系主键
     * @return 结果
     */
    @Override
    public int deleteElderFamiliesById(Long id)
    {
        return elderFamiliesMapper.deleteElderFamiliesById(id);
    }
}
